﻿using System;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using System.Linq;
using MiniWeb.Web.Controllers.Code;
using MiniWeb.Web.Controllers.Web.web.ViewModels;
using WFramework;
using WFramework.Common;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.Web.Controllers.Web.Goods
{
    public class GoodsController:BaseController
    {
        private readonly ProductsTypeTask _productsTypeTask = new ProductsTypeTask();
        private readonly ProductsClassTask _productsClassTask = new ProductsClassTask();
        private readonly ProductsInfoTask _productsInfoTask = new ProductsInfoTask();
        private readonly ProductsImgInfoTask _productsImgInfoTask = new ProductsImgInfoTask();

        public ActionResult List(int? rid,int? cid,int? pl,int pageindex=1,int pagesize =16)
        {
            rid = rid ?? 0;
            ViewData["classlist"] = _productsClassTask.GetListByTID((int)rid);
            var typeinfo = new ProductsType();
            var classinfo = new ProductsClass();
            typeinfo =  _productsTypeTask.GetById((int)rid);
            if(cid!=null)
            {
                classinfo = _productsClassTask.GetById((int)cid);
            }
            ViewData["typeinfo"] = typeinfo;
            ViewData["classinfo"] = classinfo;


            
            var pagedList = _productsInfoTask.GetPagedList(rid, cid, null,true, false,false, false,null,null,pl, pageindex, pagesize);

            var model = new PagedList<ProductsInfoDto>(pagedList.ToList(), pageindex, pagesize, pagedList.TotalItemCount);

            return View(model);
        }

        public ActionResult Search()
        {
            return View();
        }
        public ActionResult Show(int pid)
        {
            var model = _productsInfoTask.GetById(pid);
            
            if(model==null || !model.IsSell)
            {
                return RedirectToAction("Index", "home");
            }
            ViewData["typeinfo"] = _productsTypeTask.GetById(model.ProductsTypeID);
            ViewData["classinfo"] = _productsClassTask.GetById(model.ProductsClassID);


            ViewData["typelist"] = _productsClassTask.GetClass();
            ViewData["imglist"] = _productsImgInfoTask.GetAll(pid);
            ViewData["tuijianPro"] = _productsInfoTask.GetPagedList(null, null, null, true, false, true, false, null,
                                                                   null, null, 1, 4);

            return View(model);
        }


    }
}
